package com.example.android.commitcontent.ime.activities;

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.PopupWindow;

import com.example.android.commitcontent.ime.R;
import com.example.android.commitcontent.ime.cores.adapters.GeneralFragmentPagerAdapter;
import com.example.android.commitcontent.ime.cores.base.BaseActivity;
import com.example.android.commitcontent.ime.cores.utils.PopupHelper;
import com.example.android.commitcontent.ime.fragments.HanZiStatisticsManageFragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

public class HanZiStatisticsManageActivity extends BaseActivity {

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;
    private HanZiStatisticsManageFragment mCharacterFragment;
    private HanZiStatisticsManageFragment mWordFragment;

    public static void start(Context context) {
        context.startActivity(new Intent(context, HanZiStatisticsManageActivity.class));
    }

    @Override
    protected int setLayoutRes() {
        return R.layout.activity_han_zi_statistics_manage;
    }

    private String[] mPopupTitles = {"字库管理", "词库管理"};
    private List<String> mCharacterMenuList = new ArrayList<>();
    private List<String> mWordMenuList = new ArrayList<>();

    {
        Collections.addAll(mCharacterMenuList, "更新本地字库", "上传字库");
        Collections.addAll(mWordMenuList, "更新本地词库", "上传词库");
    }

    @Override
    protected void initView() {
//        showTitleMoreButton();
        hideTitleMoreButton();
        setTitleBarText("中文库统计(实验性)");
        overrideToolbarMoreButtonEvent(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isCharacterPage = mViewPager.getCurrentItem() == 0;
                PopupHelper.newInstance(HanZiStatisticsManageActivity.this)
                        .setTitleText(isCharacterPage ? mPopupTitles[0] : mPopupTitles[1])
                        .setDataList(isCharacterPage ? mCharacterMenuList : mWordMenuList, null)
                        .setItemListener(new PopupHelper.OnItemClickListener() {
                            @Override
                            public void onItemClick(PopupWindow popupWindow, int position) {
                                popupWindow.dismiss();
                                switch (position) {
                                    case 0:
                                        if (isCharacterPage)
                                            mCharacterFragment.updateCharacterDict();
                                        else
                                            mWordFragment.updateWordDict();
                                        break;
                                    case 1:
                                        if (isCharacterPage)
                                            mCharacterFragment.uploadCharacterDict();
                                        else
                                            mWordFragment.uploadWordDict();
                                        break;
                                }
                            }
                        }).showAsDropDown(view, -160, 0, true);
            }
        });
        List<Fragment> fragments = new ArrayList<>();
        mCharacterFragment = HanZiStatisticsManageFragment.newInstance(HanZiStatisticsManageFragment.TYPE_LETTERS);
        mWordFragment = HanZiStatisticsManageFragment.newInstance(HanZiStatisticsManageFragment.TYPE_WORDS);
        fragments.add(mCharacterFragment);
        fragments.add(mWordFragment);
        List<String> titles = new ArrayList<>();
        titles.add("字库统计");
        titles.add("词库统计");
        mViewPager.setAdapter(new GeneralFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles));
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1)
                    showTitleMoreButton();
                else
                    hideTitleMoreButton();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {

    }
}
